<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('User');
Globals::requireTable('Product');
Globals::requireTable('Order');
Globals::requireTable('Days');
Globals::requireTable('Pay');

class UserController extends Controller
{
	protected $UserTable;
	protected $ProductTable;
	protected $OrderTable;
	protected $DaysTable;
	protected $PayTable;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> false,
		'layoutEnabled'	=> false,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
	    $config['viewEnabled'] = false;
	    $config['layoutEnabled'] = false;
		parent::__construct($config);
		$this->UserTable 	= new UserTable($config);
		$this->ProductTable = new ProductTable($config);
		$this->OrderTable   = new OrderTable($config);
		$this->DaysTable    = new DaysTable($config);
		$this->PayTable     = new PayTable($config);
	}
	
	public function indexAction()
	{
		
	}
	
	public function orderlistAction()
	{
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 1 : $page;
	    $pagesize = $this->getIntParam('pagesize');
	    $pagesize = ($pagesize < 1 || $pagesize > 20) ? 20 : $pagesize;
	    $uid = $this->getIntParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getIntParam('machine_type');
	    
	    if($uid < 1){
	        $usermodel = new UserModel();
	        $uid = $usermodel->getUserIdByToken($token,$machine_type);
	    }
	    
	    if(empty($uid)){
	        echo json_encode(array('returncode'=>-999,'orderid'=>''));
	        exit();
	    }
	    
	    $time = time();
	    $result = array();
	    $orders = $this->OrderTable->listPage(array('uid'=>$uid),'id desc',$pagesize,$page);
	    foreach ($orders as $v){
	        $day = $this->DaysTable->getRow($v['did']);
	        
	        $tmp['orderid'] = $v['orderid'];
	        $tmp['name'] = $day['name'];
	        $tmp['money'] = $day['money'];
	        $tmp['daystart'] = date('Y-m-d',$v['starttime']);
	        $tmp['dayend'] = date('Y-m-d',$v['pasttime']);
	        $tmp['status'] = $v['status'];
	        if($tmp['status'] > 0){
	            $tmp['status'] = ($v['starttime'] < $time && $v['pasttime'] > $time) ? 1 : 2;
	        }
	        $result[] = $tmp;
	    }
	    echo json_encode(array('returncode'=>0,'list'=>$result));
	    exit();
	}
	
	public function signAction(){
	    echo json_encode(array('returncode'=>-999,'uid'=>0));
	    exit();
	    $token = $this->getParam('token');
	    $machine_type= $this->getIntParam('machine_type');
	    //$machine = $this->getParam('machine');
	    if(empty($token)){
	        echo json_encode(array('returncode'=>-999,'uid'=>0));
	        exit();
	    }
	    /*
	    $str_1 = substr($token, -7);
	    $new_num_arr = array();
	    for ($ii = 0 ; $ii < strlen($str_1) ; $ii++){
	        $tmp_num = substr($str_1, $ii , 1);
	        $new_num_arr[] = $tmp_num;
	    }
	    krsort($new_num_arr);
	    $str_2 = '';
	    foreach ($new_num_arr as $v){
	        $str_2 .= (string)$v;
	    }
	    $str_2 = $str_2 + 6387612;
	    $str_2 = substr($str_2, 1);
	    $new_num_1 = substr($str_2, 0,-2);
	    $new_num_2 = substr($str_2, -1);
	     
	    $machine_1 = substr($machine, 0,-3);
	    $machine_2 = substr($machine, -1);
	     
	    if($new_num_1 == $machine_1 && $new_num_2 == $machine_2){
	    */
    	    $user = $this->UserTable->getRow(array('token'=>$token));
    	    if($user['id'] < 1){
    	        $data['nickname'] = substr($token, 3,6).rand(1000, 9999);
    	        $data['username'] = substr($token, 3,6).rand(1000, 9999);
    	        $data['token'] = $token;
    	        $data['machine_type'] = (int)$machine_type;
    	        $user['id'] = $this->UserTable->add($data,true);
    	    }
    	    
    	    echo json_encode(array('returncode'=>0,'uid'=>$user['id']));
    	    exit();
	    /*}else{
	        echo json_encode(array('returncode'=>-999,'uid'=>0));
	        exit();
	    }*/
	}
	
	protected function out()
	{
		parent::out();
	}
}

Config::extend('UserController', 'Controller');
